package main;

import main.util.ObjectExcelRead;
import main.util.PageData;
import main.util.PropertiesUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import java.io.File;
import java.util.*;

import static main.util.FileZip.getFileType;

/**
 * 读取excel文件，对数据进行处理
 * @author se7en
 */
public class DataProcess {
    /**
     * 获取jar包绝对路径
     */
    private String jarWholePath = DataProcess.class.getProtectionDomain().getCodeSource().getLocation()
            .getFile().substring(1).replace("dataProcess.jar","");
    private String propertiesPath = jarWholePath + File.separator + "resources/report.properties";

    public static Log log = LogFactory.getLog(DataProcess.class);
    {
        PropertyConfigurator.configure(jarWholePath + File.separator
                + "resources" + File.separator + "log4j.properties");
    }


    public static void main(String[] args) throws Exception {

        DataProcess process = new DataProcess();
        String sourceDirector = process.jarWholePath + PropertiesUtil.getValue(process.propertiesPath, "sourceDirector");
        String splitDirector = process.jarWholePath + PropertiesUtil.getValue(process.propertiesPath, "splitDirector");

        // 第一步文件夹
        List<PageData> sourceList = process.readDirector(sourceDirector);
        // 第二步文件夹
        List<PageData> splitList = process.readDirector(splitDirector);

        List<PageData> outList = new ArrayList<>();
        Map<String, List<PageData>> departmentListMap = new HashMap<>();
        if (sourceList.size() != 0) {
            List<PageData> list = (List<PageData>) sourceList.get(0).get("sheetData");
            outList = process.dealTableData(list);
        } else if (splitList.size() != 0) {
            List<PageData> list = (List<PageData>) splitList.get(0).get("sheetData");
            departmentListMap = process.splitTableList(list);
        } else {
            log.info("两个文件夹下都没有excel文件！");
            System.out.println("两个文件夹下都没有excel文件！");
        }

        // 第一步导出数据
        if (outList.size() != 0) {
            String fileName = "新文件生成";
            process.exportExcel(fileName, outList);
        }

        // 第二部拆分数据
        if (departmentListMap.size() != 0) {
            for (String key : departmentListMap.keySet()) {
                String fileName = key + "-文件生成";
                List<PageData> department = departmentListMap.get(key);
                process.exportExcel(fileName, department);
            }
        }

    }

    /**
     * 提取数据，开始处理数据
     * @param sourceDirector 源文件夹
     * @throws Exception error
     */
    private List<PageData> readDirector(String sourceDirector) throws Exception {
        List<PageData> listData = new ArrayList<>();
        //获取本地文件夹下所有文件，解压zip文件
        File file = new File(sourceDirector);
        if(file.exists()) {
            File[] fs = file.listFiles();
            //判断文件存在，且文件大于0KB
            if((fs != null ? fs.length : 0) != 0) {
                for (File f : fs) {
                    if (f.isFile() && f.length() != 0) {
                        //获取文件类型
                        String type = getFileType(f.getName());
                        // 只读取第一个excel文件
                        if ("xlsx".equals(type) || "xls".equals(type)) {
                            log.info("获取文件：" + f.getName());
                            // 读取excel数据
                            ObjectExcelRead excelRead = new ObjectExcelRead();
                            listData = excelRead.readExcel(sourceDirector,f.getName() , 0, 0);
                            break;
                        } else {
                            log.error("文件夹中没有获取到excel文档！");
                        }
                    }
                }
            }
        }
        return listData;
    }

    /**
     * 导出数据
     * @param fileName 文件名
     * @param dealTableData 数据源
     */
    private void exportExcel(String fileName,List<PageData> dealTableData) {
        ObjectExcelRead excelRead = new ObjectExcelRead();
        // 导出处理后的数据
        String[] titleMap = {"结算院区","序号","单据号","意见书编码","项目名称","医保内金额","扣除金额","扣除原因","规则名称","数量","医生姓名","入院日期",
                "出院日期","费用日期","住院号","就医方式","结算日期","统筹区名称","版本号","患者姓名","出院科室","反馈申诉"};
        String[] keyMap = {"var0","var1","var2","var3","var4","var5","var6","var7","var8","var9","var10","var11","var12","var13","var14","var15","var16",
                "var17","var18","var19","var20","var21"};

        excelRead.exportToExcel(fileName,titleMap,keyMap,dealTableData,9);

        log.info(fileName + "导出完成");
        System.out.println(fileName + "导出完成");
    }


    /**
     * 功能一
     * 处理数据，工具需求处理excel表中的数据，传回到list中，导出
     * @param listData 初始数据
     * @return 输出的数据
     */
    private List<PageData> dealTableData(List<PageData> listData) {
        //移除第一列标题列，方便循环数据
        listData.remove(0);
        List<PageData> checkList = new ArrayList<>();
        for (PageData data: listData) {
            String number = data.getString("var3");
            String courtyard;

            String eastCourtyard = "994576";
            String baseCourtyard = "990053";
            if (number.contains(eastCourtyard)) {
                courtyard = "东院";
            } else if (number.contains(baseCourtyard)) {
                courtyard = "本部";
            } else {
                courtyard = "其他";
            }

            PageData checkData = new PageData();
            checkData.put("var0", courtyard);
            checkData.put("var1", data.getString("var0"));
            checkData.put("var2", data.getString("var2"));
            checkData.put("var3", data.getString("var3"));
            checkData.put("var4", data.getString("var5"));
            checkData.put("var5", data.getString("var7"));
            checkData.put("var6", data.getString("var9"));
            checkData.put("var7", data.getString("var10"));
            checkData.put("var8", data.getString("var8"));
            checkData.put("var9", data.getString("var6"));
            checkData.put("var10", data.getString("var12"));

            checkData.put("var11", data.getString("var15"));
            checkData.put("var12", data.getString("var16"));
            checkData.put("var13", data.getString("var17"));
            checkData.put("var14", data.getString("var18"));
            checkData.put("var15", data.getString("var19"));
            checkData.put("var16", data.getString("var20"));
            checkData.put("var17", data.getString("var24"));
            checkData.put("var18", data.getString("var25"));
            checkData.put("var19", "");
            checkData.put("var20", "");
            checkData.put("var21", data.getString("var26"));
            checkList.add(checkData);

        }

        return checkList;
    }

    /**
     * 功能二
     * 拆分数据，将List表中的数据以科室分类输出成不同的集合
     * @param listData 元数据
     * @return 数据集合
     */
    private Map<String,List<PageData>> splitTableList(List<PageData> listData) {
        Map<String, List<PageData>> map = new HashMap<>();
        for (PageData data :
                listData) {
            List<PageData> departmentList = map.get(data.getString("var20"));
            if (departmentList == null) {
                departmentList = new ArrayList<>();
                departmentList.add(data);
                map.put(data.getString("var20"), departmentList);
            } else {
                departmentList.add(data);
            }
        }

        return map;
    }


}


